// pages/login/login.js
const app = getApp()

Page({
  data: {
    showAccountLogin: false,
    showRegisterForm: false,
    phone: '',
    password: '',
    registerPhone: '',
    code: '',
    registerPassword: '',
    confirmPassword: '',
    agreed: false,
    codeCountdown: 0
  },

  onLoad(options) {
    // 检查是否已登录
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      wx.switchTab({
        url: '/pages/index/index'
      })
    }
  },

  // 微信授权登录
  onGetUserInfo(e) {
    if (e.detail.userInfo) {
      const userInfo = e.detail.userInfo
      
      // 模拟登录成功
      wx.showLoading({
        title: '登录中...',
      })
      
      setTimeout(() => {
        // 保存用户信息
        wx.setStorageSync('userInfo', userInfo)
        
        // 检查是否需要完善健康信息
        const healthInfo = wx.getStorageSync('healthInfo')
        if (!healthInfo) {
          wx.navigateTo({
            url: '/pages/user-setting/user-setting'
          })
        } else {
          wx.switchTab({
            url: '/pages/index/index'
          })
        }
        
        wx.hideLoading()
        app.showSuccess('登录成功')
      }, 1500)
    } else {
      app.showError('授权失败')
    }
  },

  // 显示账号登录表单
  showAccountLogin() {
    this.setData({
      showAccountLogin: true,
      showRegisterForm: false
    })
  },

  // 显示注册表单
  showRegisterForm() {
    this.setData({
      showAccountLogin: false,
      showRegisterForm: true
    })
  },

  // 输入框事件
  onPhoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  onPasswordInput(e) {
    this.setData({
      password: e.detail.value
    })
  },

  onRegisterPhoneInput(e) {
    this.setData({
      registerPhone: e.detail.value
    })
  },

  onCodeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },

  onRegisterPasswordInput(e) {
    this.setData({
      registerPassword: e.detail.value
    })
  },

  onConfirmPasswordInput(e) {
    this.setData({
      confirmPassword: e.detail.value
    })
  },

  // 协议勾选
  onAgreementChange(e) {
    this.setData({
      agreed: e.detail.value.length > 0
    })
  },

  // 获取验证码
  getCode() {
    if (!this.data.registerPhone) {
      app.showError('请输入手机号')
      return
    }

    if (!/^1[3-9]\d{9}$/.test(this.data.registerPhone)) {
      app.showError('请输入正确的手机号')
      return
    }

    // 模拟发送验证码
    app.showLoading('发送中...')
    
    setTimeout(() => {
      this.setData({
        codeCountdown: 60
      })
      
      // 开始倒计时
      const timer = setInterval(() => {
        this.setData({
          codeCountdown: this.data.codeCountdown - 1
        })
        
        if (this.data.codeCountdown <= 0) {
          clearInterval(timer)
        }
      }, 1000)
      
      wx.hideLoading()
      app.showSuccess('验证码已发送')
    }, 1000)
  },

  // 账号密码登录
  onAccountLogin() {
    if (!this.data.phone || !this.data.password) {
      app.showError('请输入手机号和密码')
      return
    }

    if (!/^1[3-9]\d{9}$/.test(this.data.phone)) {
      app.showError('请输入正确的手机号')
      return
    }

    if (this.data.password.length < 6) {
      app.showError('密码长度不能少于6位')
      return
    }

    // 模拟登录
    app.showLoading('登录中...')
    
    setTimeout(() => {
      const userInfo = {
        nickName: '用户' + this.data.phone.slice(-4),
        avatarUrl: '/images/default-avatar.png'
      }
      
      wx.setStorageSync('userInfo', userInfo)
      
      // 检查是否需要完善健康信息
      const healthInfo = wx.getStorageSync('healthInfo')
      if (!healthInfo) {
        wx.navigateTo({
          url: '/pages/user-setting/user-setting'
        })
      } else {
        wx.switchTab({
          url: '/pages/index/index'
        })
      }
      
      wx.hideLoading()
      app.showSuccess('登录成功')
    }, 1500)
  },

  // 注册
  onRegister() {
    if (!this.data.registerPhone || !this.data.code || !this.data.registerPassword || !this.data.confirmPassword) {
      app.showError('请填写完整信息')
      return
    }

    if (!/^1[3-9]\d{9}$/.test(this.data.registerPhone)) {
      app.showError('请输入正确的手机号')
      return
    }

    if (this.data.registerPassword.length < 6) {
      app.showError('密码长度不能少于6位')
      return
    }

    if (this.data.registerPassword !== this.data.confirmPassword) {
      app.showError('两次输入的密码不一致')
      return
    }

    if (!this.data.agreed) {
      app.showError('请同意用户协议和隐私政策')
      return
    }

    // 模拟注册
    app.showLoading('注册中...')
    
    setTimeout(() => {
      const userInfo = {
        nickName: '用户' + this.data.registerPhone.slice(-4),
        avatarUrl: '/images/default-avatar.png'
      }
      
      wx.setStorageSync('userInfo', userInfo)
      
      wx.navigateTo({
        url: '/pages/user-setting/user-setting'
      })
      
      wx.hideLoading()
      app.showSuccess('注册成功')
    }, 1500)
  },

  // 查看协议
  viewAgreement() {
    wx.showModal({
      title: '用户协议',
      content: '这里是用户协议内容...',
      showCancel: false
    })
  },

  viewPrivacy() {
    wx.showModal({
      title: '隐私政策',
      content: '这里是隐私政策内容...',
      showCancel: false
    })
  }
})